package com.haierCamera.customapplication.ui.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;


import com.haierCamera.customapplication.utils.DensityUtil;

import androidx.annotation.ColorInt;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 简单RecyclerView ItemDecoration
 * @author yawei
 * @data on 2018/7/27 上午10:48
 * @email zyawei@live.com
 */
public class LinearLayoutColorDivider extends RecyclerView.ItemDecoration {
    //private final int MARGIN_LEFT = 0;
    private Drawable mDivider;
    private int dividerHeight = 1;
    private int decorationHeight = 1;

    public LinearLayoutColorDivider(Context context) {
        this(context, 0xffF8F8F8, 1, 1);
    }

    public LinearLayoutColorDivider(Context context, @ColorInt int color, int decorationHeight, int dividerHeight) {
        this.mDivider = new ColorDrawable(color);
        this.dividerHeight = DensityUtil.dip2px(context, dividerHeight);
        this.decorationHeight = DensityUtil.dip2px(context, decorationHeight);
        if (dividerHeight > decorationHeight) {
            throw new IllegalArgumentException("dividerHeight [" + dividerHeight + "] > decorationHeight[" + decorationHeight+"]");
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int top;
        int bottom;

        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int itemCount = parent.getAdapter().getItemCount();
        final int childCount = parent.getChildCount();

        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            int adapterPosition = parent.getChildAdapterPosition(child);
            if (adapterPosition != itemCount - 1) {
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                int detop = (decorationHeight - dividerHeight) / 2;
                top = child.getBottom() + params.bottomMargin + detop;
                bottom = top + dividerHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        final int itemCount = parent.getAdapter().getItemCount();
        int adapterPosition = parent.getChildAdapterPosition(view);
        if (adapterPosition != itemCount - 1) {
            outRect.set(0, 0, 0, decorationHeight);
        }
    }

    void drawDivider(Canvas c, View child, String text) {

       /* Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setTextSize(TEXT_SIZE);
        final int top = child.getBottom();
        Rect rect = new Rect(0, top, 320, top + BOTTOM_HEIGHT);
        paint.setColor(0xdd808080);
        c.drawLine(0, rect.centerY(), 110, rect.centerY(), paint);
        c.drawLine(210, rect.centerY(), 320, rect.centerY(), paint);
        paint.setColor(0xff333333);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        c.drawText(text, rect.centerX() - paint.measureText(text) / 2, rect.centerY() + TEXT_SIZE / 3, paint);*/
    }
}
